package com.swlz.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.sql.SQLException;
import com.swlz.dao.MallAddressDAO;
import com.swlz.entity.MallAddress;
import com.swlz.exception.CodeException;
import com.swlz.service.MallAddressService;
import com.swlz.vo.PageVO;

/**
 * <pre>
 * 业务逻辑接口具体实现类
 * 商城用户收货地址
 * ClassName : com.swlz.service.impl.MallAddressServiceImpl
 * Author : swlz
 * E-Mail : 2233875735@qq.com
 * Date : 2020-07-02 21:59:13
 * Version : 1.0
 * Copyright 2020 swlz.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes
 * </pre>
 **/
public class MallAddressServiceImpl implements MallAddressService {
	private static Logger log = LoggerFactory.getLogger(MallAddressServiceImpl.class);
	
	private MallAddressDAO mallAddressDAO = new MallAddressDAO();

	@Override
	public int add(MallAddress mallAddress) throws CodeException {
		try {
			return mallAddressDAO.insertSelective(mallAddress);
		} catch (SQLException e) {
			log.warn("添加商城用户收货地址错误",e);
			throw new CodeException(500, "添加商城用户收货地址错误",e);
		}
	}

	@Override
	public int update(MallAddress mallAddress) throws CodeException {
		try {
			return mallAddressDAO.updateSelective(mallAddress);
		} catch (SQLException e) {
			log.warn("修改商城用户收货地址错误",e);
			throw new CodeException(501, "修改商城用户收货地址错误",e);
		}
	}

	@Override
	public int delete(Long addressId) throws CodeException {
		try {
			return mallAddressDAO.deleteById(addressId);
		} catch (SQLException e) {
			log.warn("删除商城用户收货地址错误",e);
			throw new CodeException(502, "删除商城用户收货地址错误",e);
		}
	}

	@Override
	public MallAddress get(Long addressId) throws CodeException {
		try {
			return mallAddressDAO.selectById(addressId);
		} catch (SQLException e) {
			log.warn("获取商城用户收货地址失败",e);
			throw new CodeException(503, "获取商城用户收货地址失败",e);
		}
	}

	@Override
	public PageVO<MallAddress> page(MallAddress mallAddress, Integer current, Integer rowMax) throws CodeException {
		try {
			long total = mallAddressDAO.countByModel(mallAddress);
			PageVO<MallAddress> page = new PageVO<>(total, current, rowMax);
			return page.setList(mallAddressDAO.selectByModel(mallAddress, page.getOffset(), page.getRowMax()));
		} catch (SQLException e) {
			log.warn("获取商城用户收货地址分页数据失败",e);
			throw new CodeException(504, "获取商城用户收货地址分页数据失败",e);
		}
	}

	@Override
	public List<MallAddress> list(MallAddress mallAddress, int len) {
		try {
			return mallAddressDAO.selectByModel(mallAddress, 0, len);
		} catch (SQLException e) {
			log.warn("获取商城用户收货地址列表数据失败",e);
			throw new CodeException(505, "获取商城用户收货地址列表数据失败",e);
		}	
	}

}
